/*
 * @Descripttion: 写点什么
 * @version: 
 * @Author: Zhaocy
 * @Date: 2024-07-31 10:33:52
 * @LastEditors: Zhaocy
 * @LastEditTime: 2024-07-31 14:19:16
 */
package com.zhike.service.user.impl;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.eva.epc.common.util.CommonUtils;
import com.zhike.common.conf.IMBaseConf;
import com.zhike.common.dto.R;
import com.zhike.common.dto.UserRosterVO;
import com.zhike.common.dto.UserOtherInfoDTO;
import com.zhike.common.dto.UserPasswdDTO;
import com.zhike.common.dto.UserPersonalSignatureDTO;
import com.zhike.common.dto.UserUpdateDTO;
import com.zhike.common.enums.ResultCode;
import com.zhike.common.enums.YesNoEnum;
import com.zhike.common.exception.ZkimException;
import com.zhike.common.model.IMUser;
import com.zhike.common.utils.AliSecurityCheckUtils;
import com.zhike.common.vo.UserFriendCountVO;
import com.zhike.common.vo.UserInfoVO;
import com.zhike.mapper.friends.FriendsMapper;
import com.zhike.mapper.system.DeviceMapper;
import com.zhike.mapper.user.UsersMapper;
import com.zhike.service.system.ISystemConfigService;
import com.zhike.service.system.ISystemService;
import com.zhike.service.user.IUserService;
import com.zhike.utils.cache.UsersInfoCacheProvider;

import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

  @Resource
  private IMBaseConf imBaseConf;

  @Resource
  private UsersMapper usersMapper;

  @Resource
  private DeviceMapper deviceMapper;

  @Resource
  private FriendsMapper friendsMapper;

  @Autowired
  private UsersInfoCacheProvider usersInfoCacheProvider;

  @Autowired
  private ISystemService systemService;

  @Autowired
  private ISystemConfigService systemConfigService;

  @Override
  public UserRosterVO queryUserInfo(String searchText) {
    return usersMapper.queryFriendUserInfo(searchText);
  }

  @Override
  public String queryUserMailByUid(String uid) {
    return usersMapper.queryUserMailByUid(uid);
  }

  @Override
  public UserFriendCountVO queryUserFriendCount(String uid) {
    return usersMapper.queryUserFriendCount(uid);
  }

  @Override
  public void updateOnlineStatusAll() {
    usersMapper.updateOnlineStatusAll();
  }

  @Override
  public Boolean updateOnlineStatus(Boolean isOnline, Boolean isMail, String uidOrMail, String osType) {
    return usersMapper.updateOnlineStatus(isOnline, isMail, uidOrMail, osType) > 0;
  }

  @Override
  public R<Boolean> userUpdate(String uid, UserUpdateDTO dto) {
    if (systemConfigService.getValueByKeyAsInt("securityCheck", "user_profile").equals(YesNoEnum.YES.getValue())) {
      if (!AliSecurityCheckUtils.textScan(dto.getNickName(), "nickname_detection")) {
        return R.rsp(ResultCode.IM_SECURITY_CHECK);
      }
    }
    if (usersMapper.updateUser(dto.getNickName(), dto.getSex(), uid) > 0) {
      // 更新缓存
      usersInfoCacheProvider.reloadOneCache(uid);
      return R.success(true);
    }
    return R.rsp(ResultCode.FAIL);
  }

  @Override
  public R<Boolean> userUpdatePasswd(UserPasswdDTO dto) {
    if (!systemService.checkPhoneCode(dto.getPhone(), dto.getPhoneCode())) {
      return R.rsp(ResultCode.USER_PHONECODE_INVALID);
    }

    String uid = (String) StpUtil.getLoginId();
    if (usersMapper.updatePassword(CommonUtils.encyptPassword(dto.getNewPasswd(), ""), uid) > 0) {
      return R.success(true);
    }

    return R.success(false);
  }

  @Override
  public R<Boolean> userUpdatePersonalSignature(String uid, UserPersonalSignatureDTO dto) {
    if (systemConfigService.getValueByKeyAsInt("securityCheck", "user_profile").equals(YesNoEnum.YES.getValue())) {
      if (!AliSecurityCheckUtils.textScan(dto.getSign(), "nickname_detection")) {
        return R.rsp(ResultCode.IM_SECURITY_CHECK);
      }
    }
    if (usersMapper.userUpdatePersonalSignature(dto.getSign(), uid) > 0) {
      usersInfoCacheProvider.reloadOneCache(uid);
      return R.success();
    }
    return R.rsp(ResultCode.FAIL);
  }

  @Override
  public R<Boolean> userUpdateOtherInfo(String uid, UserOtherInfoDTO dto) {
    if (usersMapper.userUpdateOtherInfo(dto.getUserDesc(), uid) > 0) {
      usersInfoCacheProvider.reloadOneCache(uid);
      return R.success();
    }
    return R.rsp(ResultCode.FAIL);
  }

  @Override
  public UserInfoVO getUserByUserId(String fakeId) {
    return usersMapper.getUserByUserId(fakeId);
  }

  @Override
  public Boolean updateUserSwitch(String uid, String way, Integer action) {
    LambdaUpdateWrapper<IMUser> luw = new LambdaUpdateWrapper<>();
    luw.eq(IMUser::getFakeUid, uid);
    switch (way) {
      case "verficate":
        luw.set(IMUser::getAddFriendVerficate, action);
        break;
      case "notify":
        luw.set(IMUser::getMessageNotify, action);
        break;
      case "account":
        luw.set(IMUser::getAllowNicknameSearch, action);
        break;
      case "phone":
        luw.set(IMUser::getAllowPhoneSearch, action);
        break;
      case "group":
        luw.set(IMUser::getAddMethodGroup, action);
        break;
      case "qr":
        luw.set(IMUser::getAddMethodQr, action);
        break;
      case "card":
        luw.set(IMUser::getAddMethodCard, action);
        break;
      default:
        log.warn("uid: {} 不支持的属性: {}", uid, way);
        throw new ZkimException(ResultCode.USER_PROPERTY);
    }

    if (usersMapper.update(new IMUser(), luw) > 0) {
      usersInfoCacheProvider.reloadOneCache(uid);
      return Boolean.TRUE;
    }
    return Boolean.FALSE;
  }

}
